package com.asia.action.ods;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.asia.service.ods.UserService;
import com.asia.domain.ods.AsiaUserTable;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;


import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginTest extends ActionSupport implements ServletResponseAware,ModelDriven<AsiaUserTable>
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private AsiaUserTable user = new AsiaUserTable();
    private UserService userService;
    private String iskeepinfo=null;

    private HttpServletRequest request;
    private HttpServletResponse response;

    public AsiaUserTable getUser() {
        return user;
    }

    @Override
    public AsiaUserTable getModel() {
        // TODO Auto-generated method stub
        return user;
    }

    public String getIskeepinfo()
    {
        return iskeepinfo;
    }

    public void setIskeepinfo(String iskeepinfo)
    {
        this.iskeepinfo = iskeepinfo;
    }


    @SuppressWarnings("unchecked")
    public String Logintst()
    {
        AsiaUserTable userDetail = null;
        response = ServletActionContext.getResponse();
        request = ServletActionContext.getRequest();
        userDetail = userService.login(user);
        DateSearchFun.setJumpctl(false);

        if (userDetail != null)
        {
            ServletActionContext.getRequest().getSession().setAttribute("cname",user.getUserName());
			/*if("keep".equals(iskeepinfo))
			{
				Cookie cookiename = new Cookie("username", user.getUserName());
				Cookie cookiepass = new Cookie("password", user.getUserPassword());
				cookiename.setMaxAge(2*7*24*3600);
				cookiepass.setMaxAge(2*7*24*3600);
				response.addCookie(cookiename);
				response.addCookie(cookiepass);
			}*/
            return SUCCESS;
        }
        else
        {
            return "falsed";
        }
    }

    public String logout()
    {
        //释放所有session，相当于注销用户
        ServletActionContext.getRequest().getSession().invalidate();
        DateSearchFun.setJumpctl(false);
        return "logout";
    }

    @Override
    public void setServletResponse(HttpServletResponse response)
    {
        // TODO Auto-generated method stub
        this.response=response;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
